//
//  TestMKUIElementViewMKUIAlertAction.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/11/15.
//  Copyright © 2023 moon. All rights reserved.
//

#import "TestMKUIElementViewMKUIAlertAction.h"
#import "MKUIElementListViewCellSelection+Test.h"
#import "MKUIElementListViewCellSwitch+Test.h"

@implementation MKUIAlertAction(Test)
- (NSString *)testDescription{
    return [NSString stringWithFormat:@"(title:%@,style:%@,isEnabled:%@)",self.title,[NSString stringWithMKUIAlertActionStyle:self.style],@(self.isEnabled)];
}
- (NSDictionary *)testJsonValue{
    NSMutableDictionary *json = [[NSMutableDictionary alloc] init];
    if(self.title) json[@"title"] = self.title;
    if(self.style) json[@"style"] = @(self.style);
    json[@"enable"] = @(self.isEnabled);
    return json;
}
+ (MKUIAlertAction *)actionWithTestJsonValue:(NSDictionary *)json{
    NSString *title = json[@"title"];
    MKUIAlertActionStyle style = [json MKUIAlertActionStyleAtPath:@"style"];
    MKUIAlertAction *action = [MKUIAlertAction actionWithTitle:title style:style handler:nil];
    action.enabled = [json mk_boolForKeyPath:@"enable" otherwise:YES];
    return action;
}
@end

@implementation TestMKUIElementViewMKUIAlertAction
- (void)setAlertAction:(MKUIAlertAction *)alertAction{
    if(_alertAction==alertAction)return;
    _alertAction = alertAction;
    [self _refreshAction];
}
- (void)_refreshAction{
    self.titleText = self.alertAction.title;
    self.subTitleText = self.alertAction.testDescription;
}
- (void)beginTextInput{
    TestMKUIAlertActionParam *vc = [[TestMKUIAlertActionParam alloc] init];
    vc.alertAction = self.alertAction;
    @MK_WEAKIFY(self);
    vc.whenDone = ^(MKUIAlertAction * _Nonnull alertAction) {
        @MK_NORMALIZE(self);
        self.alertAction = alertAction;
        [self.mk_nearestAttributeViewInListView mk_reloadInListView];
        [self.mk_navigationControllerOfFirst popViewControllerAnimated:YES];
        if(self.whenDone){
            self.whenDone(self);
        }
    };
    [self.mk_navigationControllerOfFirst pushViewController:vc animated:YES];
}
- (UIView *)deleteButtonWithAction:(void(^)(TestMKUIElementViewMKUIAlertAction *el))handler{
    MKUIFlowLayoutButton *button = [[MKUIFlowLayoutButton alloc] init];
    [button setTitle:@"删除" forState:(UIControlStateNormal)];
    [button setTitleColor:UIColor.systemRedColor forState:(UIControlStateNormal)];
    [button mk_addClickActionBlock:^(TestMKUIElementViewMKUIAlertAction *self) {
        if(handler){
            handler(self);
        }
        [self.mk_listView deleteItem:self.mk_nearestAttributeViewInListView withAnimation:(MKUIListViewItemAnimationAutomatic)];
    } context:self];
    return button;
}
@end

@implementation TestMKUIElementListViewCellMKUIAlertAction
+ (Class)elementViewClass{
    return TestMKUIElementViewMKUIAlertAction.class;
}
- (void)didSelected:(BOOL)selected{
    [super didSelected:selected];
    if(!selected) return;
    [self.elementView beginTextInput];
}
@end


@interface TestMKUIAlertActionParam()
@property (nullable, nonatomic, strong) NSString *actionTitle;
@property (nonatomic, assign) MKUIAlertActionStyle actionStyle;
@property (nonatomic, getter=isEnabled) BOOL actionEnabled;
@property(nonatomic,strong) UIButton *doneButton;
@end
@implementation TestMKUIAlertActionParam
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"设置MKUIAlertAction";
    self.listView.style = MKUIListViewStyleInsetGrouped;
    [self reloadData];
    
    self.doneButton = [[UIButton alloc] init];
    [self.doneButton setTitle:@"完成" forState:(UIControlStateNormal)];
    [self.doneButton setTitleColor:[UIColor systemBlueColor] forState:(UIControlStateNormal)];
    self.doneButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.doneButton addTarget:self action:@selector(__onDoneButtonTap:) forControlEvents:UIControlEventTouchUpInside];
    [self.doneButton sizeToFit];
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithCustomView:self.doneButton];
    self.navigationItem.rightBarButtonItem = doneItem;
}
- (void)setAlertAction:(MKUIAlertAction *)alertAction{
    _alertAction = alertAction;
    self.actionTitle = self.alertAction.title;
    self.actionStyle = self.alertAction.style;
    self.actionEnabled = self.alertAction.isEnabled;
    [self reloadData];
}
- (void)__onDoneButtonTap:(id)sender{
    if(self.whenDone){
        MKUIAlertAction *alertAction = [MKUIAlertAction actionWithTitle:self.actionTitle?:@"" style:self.actionStyle handler:nil];
        alertAction.enabled = self.actionEnabled;
        self.whenDone(alertAction);
    }
}
- (void)reloadData{
    @MK_WEAKIFY(self);
    [self.listView deleteAllSections];
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"title" selected:self.actionTitle whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.actionTitle = selectedValue;
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testElementMKUIAlertActionStyle:@"style" selected:self.actionStyle whenDone:^(MKUIAlertActionStyle selectedValue) {
            @MK_NORMALIZE(self);
            self.actionStyle = selectedValue;
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"enabled" on:self.actionEnabled whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.actionEnabled = on;
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"完成";
        el.elementView.buttonStyle = MKUIElementViewButtonStyleDefault;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            [self __onDoneButtonTap:nil];
        };
        [self.listView addItem:el];
    }
}
@end
